<?php class historys extends Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function truncate(){
    	return $this->db->truncate('historys');
    }
    
    function insert($array){    	
     $result = $this->db->insert('historys',$array);
     return $result;
    }
    
    function insert_for_excel($array){
		     if($array['type']=='1'){	
		        $conds = array(
				        		'sendtime'=>trim($array['sendtime']),
				    			's_codes'=>trim($array['s_codes']),
				    			'mod_id'=>trim($array['mod_id']),
				    			'category_id'=>trim($array['category_id']),
				    			'version_id'=>trim($array['version_id']),
				    			'company_id'=>trim($array['company_id']),
				    		);
		     }else if($array['type']=='2'){
		     	$conds = array(
				     			'r_codes'=>trim($array['r_codes']),
				     			'receivetime'=>trim($array['receivetime']),
				    			'mod_id'=>trim($array['mod_id']),
				    			'category_id'=>trim($array['category_id']),
				    			'version_id'=>trim($array['version_id']),
				     		 );
		
		     }   
    	$re = $this->select_by_array($conds);
    	
    	if(count($re)==0){
    		$this->count = $array['count'];
    		$this->db->insert('historys',$array);
    		$result = $this->db->insert_id();
    	}else{
    		
    		$count = $re[0]->count+$array['count'];
    		
    		$this->update($re[0]->h_id,array('count'=>$count));
    		$result = $re[0]->h_id;
    	}
    	return $result;
    }
    
    //send
    
    	function getsend_array($arr){
    		$this->db->limit(1);
    		$this->db->where($arr);
    		$this->db->order_by("s_r_id", "desc");
    		$query = $this->db->get('historys');
    		$result = $query->result();
    		return $result;
    	}
    
    	function main_select_chart($start,$end){
    
    		$sql = "select DISTINCT category_id from historys ";
    		if($start!=''){
    			$sql.= " where createtime between ".$start." and ".$end ;
    		}
    		$query = $this->db->query($sql);
    		$result = $query->result();
    		return $result;
    	}
    
    
    	function sel($array){
    		$this->db->where($array);
    		$this->db->select_sum('count');
    		$query = $this->db->get('historys');
    		$result = $query->result();
    		 
    		return $result;
    	}
    
    	function main_select_com($array){
    
    		$sql = "select DISTINCT company_id from historys where ".$array ;
    		$query = $this->db->query($sql);
    		$result = $query->result();
    		return $result;
    	}
    
    
    	function select_by_version($update,$arr){
    		if($update==0){
    			$this->db->where_in('version_id', $arr);
    		}else{
    			$this->db->where_not_in('version_id', $arr);
    		}
    		$query = $this->db->get('historys');
    		$result = $query->result();
    		return $result;
    	}
    
    	function main_select_update($start,$end){
    
    		$sql = "select DISTINCT category_id from historys where is_update!=0 ";
    		if($start!=''){
    			$sql.= " and createtime between ".$start." and ".$end ;
    		}
    		$query = $this->db->query($sql);
    		$result = $query->result();
    		return $result;
    	}
    
    	function main_selectupdate($ar){
    
    		$sql = "select DISTINCT category_id from historys where ".$ar;
    		$query = $this->db->query($sql);
    		$result = $query->result();
    		return $result;
    	}
    
    
    	function main_select(){
    
    		$sql = "select DISTINCT company_id from historys " ;
    		$query = $this->db->query($sql);
    		$result = $query->result();
    		return $result;
    	}
    
    
    
    	function select($id,$num="",$offset=""){
    		$this->db->limit($num,$offset);
    		if($id!=""){
    			$this->db->where($id);
    		}
    		$this->db->order_by('h_id','desc');
    		$query = $this->db->get('historys');
    		 
    		$result = $query->result();
    		//echo $this->db->last_query();
    		return $result;
    	}
    
    
    	function select_by_array($array){
    		$this->db->where($array);
    		$this->db->order_by('s_r_id','desc');
    		$query = $this->db->get('historys');
    		$result = $query->result();
    		return $result;
    	}
    
    	function getcom($id){
    		$this->db->select('company_id');
    		$this->db->where(array('category_id'=>$id));
    		$this->db->distinct();
    		$query = $this->db->get('historys');
    		$result = $query->result();
    		return $result;
    	}
    
    	function select_join($ar){
    		$this->db->select('*');
    		$this->db->where($ar);
    		$this->db->from('historys');
    		$this->db->join('category', 'historys.category_id = category.category_id');
    		$query = $this->db->get();
    		$result = $query->result();
    		return $result;
    
    	}
    
    	function getlast($version_id,$id){
    		$this->db->where(array('category_id'=>$id,'version_id'=>$version_id));
    		$this->db->select_sum('count');
    		$query = $this->db->get('historys');
    		$result = $query->result();
    		return $result;
    	}
    	function getlast_fo($version_id,$id){
    
    		$this->db->where('category_id',$id);
    		$this->db->where('version_id !=',$version_id);
    		$this->db->select_sum('count');
    		$query = $this->db->get('send');
    		$result = $query->result();
    		return $result;
    	}
    
    	function getcount($id,$version_id=''){
    		$where['category_id']=$id;
    		if($version_id!=""){
    			$where['version_id']=$version_id;
    		}
    		$this->db->where($where);
    		$this->db->select_sum('count');
    		$query = $this->db->get('historys');
    		$result = $query->result();
    		return $result;
    	}
    
    
    	function send_update($id,$array){
    		$result = $this->db->update('historys',$array,array('s_r_id'=>$id));
    		 
    		return $result;
    	}
    
    
    	function send_update_array($where,$array){
    		$result = $this->db->update('historys',$array,$where);
    		return $result;
    	}
    	function del($id){
    		$result = $this->db->delete('historys',array('s_r_id'=>$id));
    		return $result;
    	}
    	
    	//receive
    	
    		function select_totalcount_chart($start,$end){
    			$sql = "select sum(count) as count from historys ";
    			if($start!=''){
    				$sql.= " where createtime between ".$start." and ".$end ;
    			}
    			$query = $this->db->query($sql);
    			$result = $query->result();
    			return $result;
    		}
    	
    		function select_categorycount_chart($start,$end){
    			$sql = "select DISTINCT category_id from historys ";
    			if($start!=''){
    				$sql.= " where createtime between ".$start." and ".$end ;
    			}
    			$query = $this->db->query($sql);
    			$result = $query->result();
    			return $result;
    		}
    	
    		function getreceive_array($arr){
     
    			$this->db->where($arr);
    			$this->db->order_by("s_r_id", "asc");
    			$query = $this->db->get('historys');
    			$result = $query->result();
    			return $result;
    		}
    	
    		function receive_update($id){
    			$this->version_id = $_POST['version_id'];
    			$this->category_id = $_POST['category_id'];
    			$this->mod_id = $_POST['mod_id'];
    			$this->count = $_POST['count'];
    			if(isset($_POST['codes'])){
    				$this->codes = $_POST['codes'];
    			}
    			if(!isset($_POST['contacter'])){
    				$this->contacter = $_POST['contacter'];
    			}
    			if(!isset($_POST['user'])){
    				$this->user = $_POST['user'];
    			}
    			$result = $this->db->update('historys',$this,array('s_r_id'=>$id));
    			return $result;
    		}
    	
    		function update($id,$array){
    			$result = $this->db->update('historys',$array,array('h_id'=>$id));
    			return $result;
    		}
    		
    		function update_ar($id,$array){
    			$result = $this->db->update('historys',$array,array('s_r_id'=>$id));
    			return $result;
    		}
    	
    		function receive_update_array($where,$version,$array){
    			$this->db->where($where);
    			$this->db->where('version_id !=',$version);
    			$result = $this->db->update('historys',$array);
    			 
    			return $result;
    		}
}    